﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using mars_client.csharp;
using mars_client.gui.page;
using mars_client.csharp.util;

namespace mars_client.gui.page
{
    /// <summary>
    /// ChatList.xaml 的交互逻辑
    /// </summary>
    public partial class ChatListPage : Page
    {
        GuiCache guic = GuiCache.Instance;
        public ChatListPage()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 创建人   : Cecil0o0
        /// 电子邮箱 : jiegithub@gmail.com
        /// 创建时间 : 2016-2-27
        /// 功能描述 : 单击箭头的交互逻辑(每个列表项对应一个page如SingleChat或Grouguichat)
        /// </summary>
        public void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            Grid grid = (Grid)label.Parent;
            string uid = grid.Uid.ToString();
            string type = grid.DataContext.ToString();
            string name = ((TextBox)grid.Children[2]).Text;
            // MessageBox.Show("uid为:" + uid+"\r\n"+"DataContext为:" + type + "\r\n"+"name："+name);
            if(type.IndexOf("Single") != -1)
            {
                SingleChatPage page = (SingleChatPage)guic.getGui(uid + type);
                MainWindow main = (MainWindow)guic.getGui("MainWindow");
                if (page == null)
                {
                    page = new SingleChatPage();
                    main.RightFrame.Content = page;
                    page.Uid = uid;

                    guic.setRightContentUid(page.Uid);
                    guic.setGui(uid + type, page);
                    //MessageBox.Show("当前打开的页面字典中的键为：" + (uid + type));
                    page.remoteName.Text = name;
                    page.headImg.Source = SysUtil.netImageSource(((Image)grid.Children[1]).Source.ToString());
                    page.intro = (TextBox)grid.Children[3];
                }
                else
                {
                    page.Dispatcher.Invoke(() =>
                    {
                        guic.setRightContentUid(page.Uid);
                        main.RightFrame.Content = page;
                        page.remoteName.Text = name;
                    });
                }
            }else
            {
                GroupChatPage page = (GroupChatPage)guic.getGui(uid + type);
                MainWindow main = (MainWindow)guic.getGui("MainWindow");
                if (page == null)
                {
                    page = new GroupChatPage();
                    main.RightFrame.Content = page;
                    page.Uid = uid;

                    guic.setRightContentUid(page.Uid);
                    guic.setGui(uid + type, page);
                    //MessageBox.Show("当前打开的页面字典中的键为：" + (uid + type));
                    page.remoteName.Text = name;
                    page.headImg.Source = SysUtil.netImageSource(((Image)grid.Children[1]).Source.ToString());
                    page.intro = (TextBox)grid.Children[3];
                }
                else
                {
                    page.Dispatcher.Invoke(() =>
                    {
                        guic.setRightContentUid(page.Uid);
                        main.RightFrame.Content = page;
                        page.remoteName.Text = name;
                    });
                }
            }
            
            // 取消红点

            
            
            grid.Background = (Brush)new BrushConverter().ConvertFrom("#D5EDFE");
            foreach(Grid g in listWrapper.Children)
            {
                ((TextBox)g.Children[7]).Text = "0";
                ((TextBox)g.Children[7]).Visibility = Visibility.Hidden;
                if (!g.Equals(grid))
                {
                    g.Background = (Brush)new BrushConverter().ConvertFrom("#F5F9FF");
                }
                    
            }
        }
        /// <summary>
        /// 创建人   : Cecil0o0
        /// 电子邮箱 : jiegithub@gmail.com
        /// 创建时间 : 2016-2-27
        /// 功能描述 : 单击×的交互逻辑
        /// </summary>
        public void Delete_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Image I = (Image)sender;
            Grid gridCard = (Grid)I.Parent;
            string uid = gridCard.Uid;
            // 删除UI Element
            StackPanel parent = (StackPanel)gridCard.Parent;
            parent.Children.Remove(gridCard);
            // 如果当前rightFrame里的Content是当前Uid对应的Page则删除
            MainWindow main = (MainWindow)guic.getGui("MainWindow");
            if (((Page)main.RightFrame.Content).Uid == uid)
            {
                main.RightFrame.Content = new DisplayPage();
            }
            // 删除内存中的键值对
            guic.removeGui(uid + "_SingleChatPage");
        }

        private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            SystemMessagePage systemMessagePage = (SystemMessagePage)guic.getGui("SystemMessagePage");
            MainWindow mw = (MainWindow)guic.getGui("MainWindow");
            if(systemMessagePage == null)
            {
                systemMessagePage = new SystemMessagePage();
            }
            mw.RightFrame.Content = systemMessagePage;
        }
    }
}
